#!/usr/bin/env python
# -*- coding: utf-8 -*-

from components.GameBoard import GameBoard
from libavg import avg
from libavg.gameapp import GameApp
from libavg.ui.button import TouchButton
from components.LevelEditor import LevelEditor
from components import Constants
from components.Help import Help

'''Break VS Out Source-Navigator:

MODULE            CLASSES
-----------------------------
Actor             Shuttle,Actor,Ball
Cannon            Cannon, Bubble, Stock, Ammunition
GameBoard         GameBoard, LevelButton
Help              Help
Level             Level, Wall, World, CollisionListener
Special           Brick, Item, Events, Meteor, Gravity, Bumper
Leveleditor       LevelEditor, BackgroundSelector, LevelSelector, ObjectSelector, ObjectPropertiesMenu, HardnessSelectionButton, RotationSelectionButton, SizeSelectionButton, Brick

'''

class BreakVSOut(GameApp):

    __BTN_SIZE = avg.Point2D(500,168)

    'Initializes the game'
    def init(self):
        
        # OCanvases to switch game modes
        self.__menu = avg.Player.get().createCanvas(id = "mainMenu", size = Constants.RESOLUTION, handleevents = True)
        self.__editor = avg.Player.get().createCanvas(id = "editor", size = Constants.RESOLUTION, handleevents = True)
        self.__help = avg.Player.get().createCanvas(id = "help", size = Constants.RESOLUTION, handleevents = True)
        self.__board = avg.Player.get().createCanvas(id = "board", size = Constants.RESOLUTION, handleevents = True)
        
        # Main Image to display OCanvases
        self.__screen = avg.ImageNode(parent=avg.Player.get().getRootNode(), href="canvas:mainMenu", size=Constants.RESOLUTION)
        
        self.__menu.autorender = False
        self.__editor.autorender = False
        self.__board.autorender = False
        self.__help.autorender = False
        
        # BGR Sound
        self.__menuBGRSound = avg.SoundNode(parent=avg.Player.get().getRootNode(), href="resources/sound/menu_bgr.mp3", loop=True, volume=Constants.MENU_VOL)
        
        LevelEditor(parent= self.__editor.getRootNode(), game=self)
        Help(parent= self.__help.getRootNode(), game=self)
        self.__gameboard = GameBoard(parent=self.__board.getRootNode(), game=self)
      
        avg.ImageNode(parent=self.__menu.getRootNode(), href="resources/textures/menu/overlay.png")
        avg.ImageNode(parent=self.__menu.getRootNode(), href="resources/textures/menu/AppName.png", pos=(100, 90), size=(self.__BTN_SIZE.x, 100))
        
        self.__startSingleButton = TouchButton(
                pos = (100, 210),
                size = self.__BTN_SIZE,
                upNode = avg.ImageNode(size = self.__BTN_SIZE, href = "resources/textures/menu/SinglePlayerBtn.png"),
                downNode = avg.ImageNode(size = self.__BTN_SIZE, href = "resources/textures/menu/SinglePlayerBtn.png"),
                clickHandler = lambda: self.__showBoard(1),
                parent = self.__menu.getRootNode())
     
        self.startMultiButton = TouchButton(
                pos = (100,  210 + self.__BTN_SIZE.y + 20),
                size = self.__BTN_SIZE,
                upNode = avg.ImageNode(size = self.__BTN_SIZE, href = "resources/textures/menu/MultiPlayerBtn.png"),
                downNode = avg.ImageNode(size = self.__BTN_SIZE, href = "resources/textures/menu/MultiPlayerBtn.png"),
                clickHandler = lambda: self.__showBoard(2),
                parent = self.__menu.getRootNode())
        
        self.editorButton = TouchButton(
                pos = (100,  210 + self.__BTN_SIZE.y * 2 + 40),
                size = (self.__BTN_SIZE.x/2, self.__BTN_SIZE.y - 20),
                upNode = avg.ImageNode(size=(self.__BTN_SIZE.x/2, self.__BTN_SIZE.y - 20), href = "resources/textures/menu/EditorBtn.png"),
                downNode = avg.ImageNode(size=(self.__BTN_SIZE.x/2, self.__BTN_SIZE.y - 20), href = "resources/textures/menu/EditorBtn.png"),
                clickHandler = self.__showEditor,
                parent = self.__menu.getRootNode())
        
        self.helpButton = TouchButton(
                pos = (100 + self.__BTN_SIZE.x/2,  210 + self.__BTN_SIZE.y * 2 + 40),
                size = (self.__BTN_SIZE.x/2, self.__BTN_SIZE.y - 20),
                upNode = avg.ImageNode(size=(self.__BTN_SIZE.x/2, self.__BTN_SIZE.y - 20), href = "resources/textures/menu/HelpBtn.png"),
                downNode = avg.ImageNode(size=(self.__BTN_SIZE.x/2, self.__BTN_SIZE.y - 20), href = "resources/textures/menu/HelpBtn.png"),
                clickHandler = self.__showHelp,
                parent = self.__menu.getRootNode())
        
        self.closeBtn = TouchButton (
                parent = self.__menu.getRootNode(),
                pos = (Constants.RESOLUTION.x - 60, 10),
                size=(50,50),
                upNode = avg.ImageNode(href = "resources/textures/menu/btn_exit.png", size=(50, 50)),
                downNode = avg.ImageNode(href = "resources/textures/menu/btn_exit_d.png", size=(50, 50)),
                clickHandler = lambda:exit(0)) 
        
        self.showMenu()
     
    def showMenu(self):
        self.__menuBGRSound.href = "resources/sound/menu_bgr.mp3"
        self.__menuBGRSound.play()
        
        self.__editor.autorender = False
        self.__editor.getRootNode().opacity = 0
        self.__board.autorender = False
        self.__board.getRootNode().opacity = 0
        self.__help.autorender = False
        self.__help.getRootNode().opacity = 0
        
        self.__screen.href = "canvas:mainMenu"
        self.__menu.autorender = True
        self.__menu.getRootNode().opacity = 1 
        
    def mute(self):
        self.__menuBGRSound.stop()
        
    def unmute(self):
        self.__menuBGRSound.play()
     
    def __hideMenu(self):
        self.__menu.getRootNode().opacity = 0
        self.__menu.autorender = False
     
    def __showBoard(self, players):
        self.__hideMenu()

        self.__gameboard.setPlayers(players)
        self.__gameboard.show()

        self.__screen.href = "canvas:board"
        self.__board.autorender = True
        self.__board.getRootNode().opacity = 1

    def __showEditor(self):
        self.__hideMenu()
        self.__menuBGRSound.href = "resources/sound/editor_bgr.mp3"
        self.__menuBGRSound.play()
        
        self.__screen.href = "canvas:editor"
        self.__editor.autorender = True
        self.__editor.getRootNode().opacity = 1
        
    def __showHelp(self):
        self.__hideMenu()
        
        self.__screen.href = "canvas:help"
        self.__help.autorender = True
        self.__help.getRootNode().opacity = 1
        
if __name__ == '__main__':
    BreakVSOut.start(resolution = Constants.RESOLUTION)
    
